package com.asistencia.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asistencia.businness.CourseBusinness;
import com.asistencia.businness.ProfessorCourseBusiness;
import com.asistencia.businness.SemesterBusiness;
import com.asistencia.businness.TypeCourseBusinness;
import com.asistencia.domain.Course;
import com.asistencia.domain.Professor;
import com.asistencia.domain.ProfessorCourse;
import com.asistencia.domain.Semester;
import com.asistencia.domain.TypeCourse;
import com.asistencia.util.Constant;

@Controller
public class ProfessorCourseController {

	@Autowired
	private ProfessorCourseBusiness professorcourseBusiness;

	@Autowired
	private SemesterBusiness semesterBusiness;

	@Autowired
	private TypeCourseBusinness typeCourseBusinness;
	
	@Autowired
	private CourseBusinness courseBusinness;
	
	@RequestMapping(value = "filter-course-by-semester.tecsup", method = RequestMethod.POST)
	public String filterCourseByProfessor(
			@RequestParam(value = "idsemester", required = true) Integer idSemester,HttpSession session,Semester semester,ModelMap modelMap) {
		
		List<Semester> lstSemesters = new ArrayList<Semester>();
		semester = semesterBusiness.getById(idSemester); ;
		Professor professor = (Professor)session.getAttribute(Constant.USER_IN_SESSION);
		List<ProfessorCourse> lstProfessorCourses = professorcourseBusiness.listSemestersByIdProfesor(professor.getIdProfessor());
		
		if(lstProfessorCourses != null){                        	
        	for (ProfessorCourse professorCourse : lstProfessorCourses) {
        		Semester temp = semesterBusiness.getById(professorCourse.getMySemester().getIdSemester());
        		lstSemesters.add(temp);
        	}
        }
		
		lstProfessorCourses = professorcourseBusiness.listProfessorCourseByProfessorSemester(professor.getIdProfessor(), semester.getIdSemester());
        
        if(lstProfessorCourses != null){
        	for (ProfessorCourse professorCourse : lstProfessorCourses) {
                professorCourse.setMyprofessor(professor);
                professorCourse.setMySemester(semester);
                
                Course course = courseBusinness.getCourseById(professorCourse.getMyCourse().getIdCourse());
                // obtenemos el tipo del curso
                TypeCourse typeCourse = typeCourseBusinness.getById(course.getMyTypeCourse().getIdTypeCourse());
                course.setMyTypeCourse(typeCourse);
                
                professorCourse.setMyCourse(course);
        	}
        }
        
        modelMap.addAttribute("semester", semester);
        modelMap.addAttribute("lstSemesters", lstSemesters);
        modelMap.addAttribute("lstProfessorCourses", lstProfessorCourses);
		
		return "professor-index";
	}
}
